package redishelper

import (
	"context"
	"gatewayConsumer/configs"
	"github.com/go-redis/redis/v8"
	log "github.com/sirupsen/logrus"
	"sync"
)

var (
	client *redis.Client
	once   sync.Once
)

func init() {
	log.Infof("init执行了吗")
	Rdb()
}

// 获取redisClient
func Rdb() *redis.Client {
	if client != nil {
		log.Infof("redis client 已经初始化了")
		return client
	}
	once.Do(func() {
		redisDto := configs.GetConfig().Redis
		client = redis.NewClient(&redis.Options{
			Addr:     redisDto.Host,
			Password: redisDto.Password, // no password set
			DB:       0,                 // use default DB
			PoolSize: 100,               //连接池大小
		})
	})

	return client
}

func GetGatewayCache() string {
	ctx := context.Background()
	r, err := client.Get(ctx, "{realidiot}:{gateway_ping}:1347B01F").Result()
	if err != nil {
		log.Errorf(err.Error())
		return ""
	}

	return r
}

func GetTagCache() string {
	ctx := context.Background()
	r, err := client.Get(ctx, "{realidiot}:{up_records}:000099000029").Result()
	if err != nil {
		log.Errorf(err.Error())
		return ""
	}

	return r

}
